package com.fortune.controller;

import java.util.List;

import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreDeficitExcution;
import com.fortune.dto.excution.MStoreSurplusExcution;
import com.fortune.dto.in.mStoreDeficit.MStoreDeficitListDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusEditDto;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreDeficitEnum;
import com.fortune.enums.MStoreSurplusEnum;
import com.fortune.exception.db.InsertInnerErrorException;
import com.fortune.service.MStoreSurplusService;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfo;
import com.fortune.vo.mStoreDeficit.MStoreDeficitInfoDetail;
import io.swagger.annotations.*;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.mStoreSurplus.MStoreSurplusAddDto;
import com.fortune.vo.mStoreSurplus.MStoreSurplusInfo;
import com.fortune.vo.mStoreSurplus.MStoreSurplusInfoDetail;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "mStoreSurplus", description = "厂商仓库报盈", tags = {"厂商仓库报盈"})
@RestController
@RequestMapping("mStoreSurplus")
public class MStoreSurplusController {
	@Autowired
	private MStoreSurplusService mStoreSurplusService;
	Logger logger= LoggerFactory.getLogger(this.getClass());
	
	@ApiOperation(value = "报盈单列表", nickname = "mStoreSurplusList", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "第几页", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "length", value = "每页容量", paramType = "query", dataType = "int"),
			@ApiImplicitParam(name = "search", value = "搜索关键词", paramType = "query", dataType = "string", required = false)
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData> list(HttpServletRequest request, @ApiIgnore @Validated MStoreSurplusListDto mStoreSurplusListDto) {
		mStoreSurplusListDto.setPage((mStoreSurplusListDto.getPage()-1)*10);
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		List<MStoreSurplusInfo> list = mStoreSurplusService.findSurplusList(companyId,mStoreSurplusListDto);
		int count = mStoreSurplusService.findSurplusListCount(companyId,mStoreSurplusListDto);
		PagingData pagingData = new PagingData();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new MStoreSurplusExcution<PagingData>(pagingData, MStoreSurplusEnum.SUCCESS);
	}
	
	@ApiOperation(value = "报盈单详情", nickname = "mStoreSurplusDetail", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="id", value="报盈单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MStoreSurplusInfoDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="报盈单id不能为空")String id) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		MStoreSurplusInfoDetail detail=mStoreSurplusService.findDetail(companyId,id);
		return new MStoreSurplusExcution<MStoreSurplusInfoDetail>(detail, MStoreSurplusEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加报盈单", nickname = "mStoreSurplusAdd", notes = "", tags={ "@岳希伟" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request ,@Validated @RequestBody MStoreSurplusAddDto addDto) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		String uid=mUserInfo.getId();
		try{
			int addSurplusBase=mStoreSurplusService.addSurplusBase(companyId,uid,addDto);
			if(addSurplusBase>0){
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
			}else {
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
			}
		}catch (InsertInnerErrorException e){
			logger.error(e.getMessage(), e);
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}
	}




	@ApiOperation(value = "确认入库", nickname = "mStoreSurplusIn", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="uuid", value="报盈单id",paramType="query",dataType="string",required=true)
	@PostMapping(value = "editIn",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> editIn(HttpServletRequest request, @Validated @NotBlank String uuid) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		String operatorId=mUserInfo.getId();
		int editMStoreSurplusConfirm=mStoreSurplusService.editMStoreSurplusConfirm(companyId,uuid,operatorId);
		if(editMStoreSurplusConfirm>0){
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
		}else {
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}
	}


	@ApiOperation(value = "删除", nickname = "mStoreSurplusDelete", notes = "", tags={ "@岳希伟" })
	@ApiImplicitParam(name="uuid", value="报盈单id",paramType="query",dataType="string",required=true)
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request, @Validated @NotBlank String uuid) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		int deleteMStoreDeficit=mStoreSurplusService.deleteDStoreSurplus(companyId,uuid);
		if(deleteMStoreDeficit>0){
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
		}else {
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}
	}


	@ApiOperation(value = "编辑报盈单", nickname = "mStoreSurplusEdit", notes = "", tags={ "@岳希伟" })
	@PostMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request, @Validated @RequestBody MStoreSurplusEditDto editDto) {
		HttpSession session=request.getSession();
		MUserInfo mUserInfo= (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId=mUserInfo.getmId();
		try{
			int editDeficitBase=mStoreSurplusService.editSurplusBase(companyId,editDto);
			if(editDeficitBase>0){
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.SUCCESS);
			}else {
				return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
			}
		}catch (InsertInnerErrorException e){
			logger.error(e.getMessage(), e);
			return new MStoreDeficitExcution<Void>(null, MStoreDeficitEnum.FAILURE);
		}

	}
	
}
